package com.qf.mybatisplus.controller;

import com.qf.mybatisplus.resultBean.JsonResult;
import com.qf.mybatisplus.resultBean.ResultCode;
import com.qf.mybatisplus.utils.DateUtil;
import com.qf.mybatisplus.utils.EnvUtils;
import com.qf.mybatisplus.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 12136
 * @version 1.0
 * @description: TODO
 * @date 2022/4/29 11:47
 */
@Api(tags = "公用功能控制器",value = "公用功能控制器")
@RestController
@RequestMapping("/common")
public class CommonController {



    @ApiOperation("文件上传")
    @PostMapping("/uploadFile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "file")
    })
    public JsonResult uploadFile(MultipartFile file){
        try {
            // 上传文件路径
            String year = DateUtil.getYear();
            String month = DateUtil.getMonth();
            String day = DateUtil.getDay();
            String nginxPath = EnvUtils.getProperty("my.img.diskPath");
            String ipPath = EnvUtils.getProperty("my.img.ipPath");
            String name = FileUtils.MultipartFileUpload(file, nginxPath + "uploadFile/" + year + "/" + month + "/" + day + "/");
            String url = ipPath + "uploadFile/" + year + "/" + month + "/" + day + name.substring(name.lastIndexOf("/"));
            // 上传并返回新文件名称
            return JsonResult.resultOk(url,"文件上传成功");
        } catch (Exception e) {
            return JsonResult.failed(ResultCode.INTERNAL_SERVER_ERROR,null,"文件上传失败");
        }
    }
}
